package com.otdemo.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.otdemo.dao.RestUserDao;
import com.otdemo.entity.RestUser;
import com.otdemo.resp.CommonParam;
import com.otdemo.resp.RestUserResp;
import com.otdemo.type.GenderEnum;
import com.otdemo.type.RoleEnum;
import com.otdemo.util.DateUtils;
import org.apache.commons.lang3.StringUtils;

@WebServlet("/user/list")
public class UserController extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//2.request和response的编码方法设置，一律UTF-8
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=utf-8");
        CommonParam param = new CommonParam();
        param.setRole(RoleEnum.user.getCode());
        req.setAttribute("list", initList(param, req));
        req.getRequestDispatcher("/User.jsp").forward(req, resp);
    }

    private List<RestUserResp> initList(CommonParam param, HttpServletRequest request) {
        List<RestUser> list = new RestUserDao().list();
        if (StringUtils.isNotBlank(param.getGender())) {
            list = list.stream().filter(p -> StringUtils.isNotBlank(p.getGender()) && p.getGender().equals(param.getGender())).collect(Collectors.toList());
        }
        if (StringUtils.isNotEmpty(param.getName())) {
            list = list.stream().filter(p -> p.getName().indexOf(param.getName().trim()) != -1).collect(Collectors.toList());
        }
        if (StringUtils.isNotEmpty(param.getMobile())) {
            list = list.stream().filter(p -> p.getMobile().equals(param.getMobile().trim())).collect(Collectors.toList());
        }
        if (StringUtils.isNotEmpty(param.getRole())) {
            list = list.stream().filter(p -> p.getRole().equals(param.getRole().trim())).collect(Collectors.toList());
        }
        List<RestUserResp> respList = new ArrayList<>();
        try {
            for (RestUser rest : list) {
                RestUserResp resp = new RestUserResp();
                resp.setUid(rest.getUid());
                resp.setId(rest.getId());
                resp.setName(rest.getName());
                resp.setUsername(rest.getUsername());
                resp.setPassword(rest.getPassword());
                resp.setEmail(rest.getEmail());
                resp.setMobile(rest.getMobile());
                resp.setAge(rest.getAge());
                resp.setCreateDate(DateUtils.dateToString(rest.getCreateDate()));
                resp.setUpdateDate(DateUtils.dateToString(rest.getUpdateDate()));
                if (StringUtils.isNotBlank(rest.getGender())) {
                    resp.setGenderName(GenderEnum.getName(rest.getGender()));
                }
                if (StringUtils.isNotBlank(rest.getRole())) {
                    resp.setRoleName(RoleEnum.getName(rest.getRole()));
                }
                if (rest.getBirthDate() != null) {
                    resp.setBirthDateName(DateUtils.dateToString(rest.getBirthDate()));
                }
                resp.setNumber(StringUtils.substring(rest.getUid(), 0, 8).toUpperCase());
                respList.add(resp);
            }
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
        return respList;
    }
}
